/*
* ref: reference to current country select
* url: url to web service
* */


$(document).ready(function(){
    $(".datepicker").datepicker({ dateFormat: 'dd/mm/y'});
});


function getRegions(ref, url){
    var countryId = ref.value;
    if(countryId != ''){
        $.ajax({
            async: false,
            traditional: true,
            type: "POST",
            url: url,
            data: { "countryId": countryId},
            success: function(result)
            {
                var options = $(result + ' #regions')[0].cells[1].children[0].options;
                var regionSelect = $(ref).parent().parent().next()[0].children[1].children[0];
                $(regionSelect).empty();
                $(regionSelect).html(options);
                $(regionSelect).parent().parent().next()[0].children[1].children[0].value = 2;
                $(regionSelect).removeAttr('disabled');
                var citiesSelect = $(regionSelect).parent().parent().next()[0].children[1].children[0];
                $(citiesSelect).attr('disabled', 'disabled');
                $(citiesSelect).empty().html('<option value="2">Select region first</option>')
            }
        });
    }
}

function getCities(ref, url, loaderUrl){
    var regionId = ref.value;
    if(regionId != ''){
        var regionSelect = $(ref).parent().parent().prev()[0].children[1].children[0];
        $(regionSelect).next().show();
        $.ajax({
            async:false,
            traditional: true,
            type: "POST",
            url: url,
            data: { "regionId": regionId},
            success: function(result)
            {
                var options = $(result + ' #cities')[0].cells[1].children[0].options;
                var citiesSelect = $(ref).parent().parent().next()[0].children[1].children[0];
                $(citiesSelect).empty();
                $(citiesSelect).html(options);
                $("#ajax-loader").remove();
                $(citiesSelect).removeAttr('disabled');
            }
        });
    }
}

function checkOther(select){
    if(select.value == -2){
        $(select).next().val('');
        $(select).next().show();
    }else{
        $(select).next().hide();
        $(select).next().val($(select).children("option:selected").text());
    }
}